﻿using System;
using System.Windows.Forms;
using QuanLyThuPhiTaiLieu.Biz;
using QuanLyThuPhiTaiLieu.Common;
using QuanLyThuPhiTaiLieu.Entity;
using System.IO;
using System.Collections.Generic;
using Telerik.WinControls.UI;
using Telerik.WinControls.Data;
using System.ComponentModel;
using System.Diagnostics;

namespace QuanLyThuPhiTaiLieu.Forms
{
    public partial class DisplayBangKeKhaiPhiSuDung : Form
    {
        private List<PhieuBanGiaoTaiLieuChiTiet> listChiTiet;
        public List<PhieuBanGiaoTaiLieuChiTiet> ListChiTiet
        {
            get
            {
                if (listChiTiet == null)
                {
                    return new List<PhieuBanGiaoTaiLieuChiTiet>();
                }
                else
                {
                    return listChiTiet;
                }
            }
            set
            {
                listChiTiet = value;
            }
        }

        private int id;

        private int attachtId;

        public DisplayBangKeKhaiPhiSuDung(int id)
        {
            InitializeComponent();

            GridViewSummaryItem total = new GridViewSummaryItem("ThanhTien", "Tổng tiền = {0: #,#}", GridAggregateFunction.Sum);

            GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem(new GridViewSummaryItem[] { total });
            radGridView1.SummaryRowsTop.Add(summaryRowItem);

            SetPreferences();
            RefreshDetailView(id);

            this.id = id;
        }
        
        private void Cancel(object sender, EventArgs e)
        {
            Utilities.CloseForm(this, new ViewBangKeKhaiPhiSuDungTaiLieu());
        }

        private void FormLoad(object sender, EventArgs e)
        {
            var parent = MdiParent as MDI;
            if (parent != null)
            {
                //Utilities.ResetBackColor(parent.TopMenu, Constants.MenuIndex.TaiLieuVanBanPhapLy);
                parent.Status.Text = Constants.FormStatus.PhieuBanGiao;
            }

            if (id > 0)
            {
                var biz = new PhieuBanGiaoTaiLieuBiz();
                var item = biz.LoadItem(id);

                textNguoiNhan.Text = item.NguoiNhan;
                textChucDanh.Text = item.ChucDanh;
                textDonVi.Text = item.DonVi;
                textDiaChi.Text = item.DiaChi;
                textThongTinChiTiet.Text = item.ThongTinChiTiet;
                textHinhThucThanhToan.Text = item.HinhThucThanhToan;
                textNguoiGiao.Text = item.NguoiGiao;
                textTpLtdc.Text = item.Tpltdc;
                textGiamDoc.Text = item.GiamDoc;
                textNgay.Text = item.NgayThang.Day.ToString();
                textThang.Text = item.NgayThang.Month.ToString();
                textNam.Text = item.NgayThang.Year.ToString();
                textSoPhieu.Text = item.SoPhieu;

                if (item.AttachmentId.HasValue && item.AttachmentId.Value > 0)
                {
                    attachtId = item.AttachmentId.Value;
                    var attBiz = new AttachmentBiz();
                    var attachment = attBiz.LoadItem(attachtId);
                    linkFile.Text = attachment.FileName;
                }
            }            
        }
                
        private void RefreshDetailView(int id)
        {
            radGridView1.DataSource = null;

            var biz = new PhieuBanGiaoTaiLieuChiTietBiz();

            if (id > 0)
            {
                listChiTiet = biz.LoadItemsByParentId(id);
            }
            
            radGridView1.DataSource = listChiTiet;
            radGridView1.Refresh();
        }

        private void ExportBangKeKhai(object sender, EventArgs e)
        {
            //var result = exportFileDialog.ShowDialog(this);
            //if (result == System.Windows.Forms.DialogResult.OK &&
            //    !string.IsNullOrEmpty(exportFileDialog.FileName) && exportFileDialog.ValidateNames)
            //{
            //    string filePath = exportFileDialog.FileName;
            //    var biz = new PhieuBanGiaoTaiLieuBiz();
            //    biz.ExportBang(id, fileOutPath: filePath);

            //    MessageBox.Show(Constants.Messages.ExportCompleted);
            //}
            var biz = new PhieuBanGiaoTaiLieuBiz();
            string filePath = Path.Combine(Path.GetTempPath(), DateTime.Now.ToString("ddMMyyyy_hhmmss") + ".xlsx");
            biz.ExportBang(id, fileOutPath: filePath);
        }

        private void ExportPhieuBanGiao(object sender, EventArgs e)
        {
            //var result = exportFileDialog.ShowDialog(this);
            //if (result == System.Windows.Forms.DialogResult.OK &&
            //    !string.IsNullOrEmpty(exportFileDialog.FileName) && exportFileDialog.ValidateNames)
            //{
            //    string filePath = exportFileDialog.FileName;
            //    var biz = new PhieuBanGiaoTaiLieuBiz();
            //    biz.ExportPhieu(id, fileOutPath: filePath);

            //    MessageBox.Show(Constants.Messages.ExportCompleted);
            //}

            var biz = new PhieuBanGiaoTaiLieuBiz();
            string filePath = Path.Combine(Path.GetTempPath(), DateTime.Now.ToString("ddMMyyyy_hhmmss") + ".xlsx");
            biz.ExportPhieu(id, fileOutPath: filePath);
        }

        private void Edit(object sender, EventArgs e)
        {
            Utilities.CloseForm(this, new NewBangKeKhaiPhiSuDung(id));            
        }

        private void InPhieuBanGiao(object sender, EventArgs e)
        {
            var biz = new PhieuBanGiaoTaiLieuBiz();
            biz.ExportPhieu(id, isPrint: true);
        }

        private void InBangKeKhai(object sender, EventArgs e)
        {
            var biz = new PhieuBanGiaoTaiLieuBiz();
            biz.ExportBang(id, isPrint: true);
        }

        private void SetPreferences()
        {
            radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            radGridView1.EnableHotTracking = true;

            this.radGridView1.MasterTemplate.EnableGrouping = true;
            this.radGridView1.MasterTemplate.AllowDragToGroup = false;
            this.radGridView1.MasterTemplate.AutoExpandGroups = true;
            this.radGridView1.ShowGroupPanel = false;
            this.radGridView1.AllowAddNewRow = false;

            GroupDescriptor descriptor2 = new GroupDescriptor();
            descriptor2.GroupNames.Add("GroupValue", ListSortDirection.Ascending);
            this.radGridView1.GroupDescriptors.Add(descriptor2);
        }

        private void OpenFile(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var biz = new AttachmentBiz();
            var path = biz.SaveAttachmentsToTempFolder(attachtId);
            Process.Start(path);
        }
    }
}
